﻿namespace TicTacToe.Core.AI
{
    static partial class Conditions
    {
        static ConditionResult OwnXThreeRow(AIGameField field, int player, int x, int y, int z)
        {
            if (x == 0)
            {
                if (field[x + 1, y, z] == player &&
                    field[x + 2, y, z] == player &&
                    field[x + 3, y, z] == player)
                    return ConditionResult.WinOpportunity;
            }
            else if (x == 3)
            {
                if (field[x - 1, y, z] == player &&
                    field[x - 2, y, z] == player &&
                    field[x - 3, y, z] == player)
                    return ConditionResult.WinOpportunity;
            }
            return ConditionResult.None;
        }

        static ConditionResult OwnYThreeRow(AIGameField field, int player, int x, int y, int z)
        {
            if (y == 0)
            {
                if (field[x, y + 1, z] == player &&
                    field[x, y + 2, z] == player &&
                    field[x, y + 3, z] == player)
                    return ConditionResult.WinOpportunity;
            }
            else if (y == 3)
            {
                if (field[x, y - 1, z] == player &&
                    field[x, y - 2, z] == player &&
                    field[x, y - 3, z] == player)
                    return ConditionResult.WinOpportunity;
            }
            return ConditionResult.None;
        }

        static ConditionResult OwnZThreeRow(AIGameField field, int player, int x, int y, int z)
        {
            if (z == 0)
            {
                if (field[x, y, z + 1] == player &&
                    field[x, y, z + 2] == player &&
                    field[x, y, z + 3] == player)
                    return ConditionResult.WinOpportunity;
            }
            else if (z == 3)
            {
                if (field[x, y, z - 1] == player &&
                    field[x, y, z - 2] == player &&
                    field[x, y, z - 3] == player)
                    return ConditionResult.WinOpportunity;
            }
            return ConditionResult.None;
        }

        static ConditionResult OwnXYThreeRow(AIGameField field, int player, int x, int y, int z)
        {
            if (x == 0)
            {
                if (y == 0)
                {
                    if (field[x + 1, y + 1, z] == player &&
                        field[x + 2, y + 2, z] == player &&
                        field[x + 3, y + 3, z] == player)
                        return ConditionResult.WinOpportunity;
                }
                else if (y == 3)
                {
                    if (field[x + 1, y - 1, z] == player &&
                        field[x + 2, y - 2, z] == player &&
                        field[x + 3, y - 3, z] == player)
                        return ConditionResult.WinOpportunity;
                }
            }
            else if (x == 3)
            {
                if (y == 0)
                {
                    if (field[x - 1, y + 1, z] == player &&
                        field[x - 2, y + 2, z] == player &&
                        field[x - 3, y + 3, z] == player)
                        return ConditionResult.WinOpportunity;
                }
                else if (y == 3)
                {
                    if (field[x - 1, y - 1, z] == player &&
                        field[x - 2, y - 2, z] == player &&
                        field[x - 3, y - 3, z] == player)
                        return ConditionResult.WinOpportunity;
                }
            }
            return ConditionResult.None;
        }

        static ConditionResult OwnXZThreeRow(AIGameField field, int player, int x, int y, int z)
        {
            if (x == 0)
            {
                if (z == 0)
                {
                    if (field[x + 1, y, z + 1] == player &&
                        field[x + 2, y, z + 2] == player &&
                        field[x + 3, y, z + 3] == player)
                        return ConditionResult.WinOpportunity;
                }
                else if (z == 3)
                {
                    if (field[x + 1, y, z - 1] == player &&
                        field[x + 2, y, z - 2] == player &&
                        field[x + 3, y, z - 3] == player)
                        return ConditionResult.WinOpportunity;
                }
            }
            else if (x == 3)
            {
                if (z == 0)
                {
                    if (field[x - 1, y, z + 1] == player &&
                        field[x - 2, y, z + 2] == player &&
                        field[x - 3, y, z + 3] == player)
                        return ConditionResult.WinOpportunity;
                }
                else if (z == 3)
                {
                    if (field[x - 1, y, z - 1] == player &&
                        field[x - 2, y, z - 2] == player &&
                        field[x - 3, y, z - 3] == player)
                        return ConditionResult.WinOpportunity;
                };
            }
            return ConditionResult.None;
        }

        static ConditionResult OwnYZThreeRow(AIGameField field, int player, int x, int y, int z)
        {
            if (y == 0)
            {
                if (z == 0)
                {
                    if (field[x, y + 1, z + 1] == player &&
                        field[x, y + 2, z + 2] == player &&
                        field[x, y + 3, z + 3] == player)
                        return ConditionResult.WinOpportunity;
                }
                else if (z == 3)
                {
                    if (field[x, y + 1, z - 1] == player &&
                        field[x, y + 2, z - 2] == player &&
                        field[x, y + 3, z - 3] == player)
                        return ConditionResult.WinOpportunity;
                }
            }
            else if (y == 3)
            {
                if (z == 0)
                {
                    if (field[x, y - 1, z + 1] == player &&
                        field[x, y - 2, z + 2] == player &&
                        field[x, y - 3, z + 3] == player)
                        return ConditionResult.WinOpportunity;
                }
                else if (z == 3)
                {
                    if (field[x, y - 1, z - 1] == player &&
                        field[x, y - 2, z - 2] == player &&
                        field[x, y - 3, z - 3] == player)
                        return ConditionResult.WinOpportunity;
                };
            }
            return ConditionResult.None;
        }

        static ConditionResult OwnXYZThreeRow(AIGameField field, int player, int x, int y, int z)
        {
            if (x == 0)
            {
                if (y == 0)
                {
                    if (z == 0)
                    {
                        if (field[x + 1, y + 1, z + 1] == player &&
                            field[x + 2, y + 2, z + 2] == player &&
                            field[x + 3, y + 3, z + 3] == player)
                            return ConditionResult.WinOpportunity;
                    }
                    else if (z == 3)
                    {
                        if (field[x + 1, y + 1, z - 1] == player &&
                            field[x + 2, y + 2, z - 2] == player &&
                            field[x + 3, y + 3, z - 3] == player)
                            return ConditionResult.WinOpportunity;
                    }
                }
                else if (y == 3)
                {
                    if (z == 0)
                    {
                        if (field[x + 1, y - 1, z + 1] == player &&
                            field[x + 2, y - 2, z + 2] == player &&
                            field[x + 3, y - 3, z + 3] == player)
                            return ConditionResult.WinOpportunity;
                    }
                    else if (z == 3)
                    {
                        if (field[x + 1, y - 1, z - 1] == player &&
                            field[x + 2, y - 2, z - 2] == player &&
                            field[x + 3, y - 3, z - 3] == player)
                            return ConditionResult.WinOpportunity;
                    }
                }
            }
            else if (x == 3)
            {
                if (y == 0)
                {
                    if (z == 0)
                    {
                        if (field[x - 1, y + 1, z + 1] == player &&
                            field[x - 2, y + 2, z + 2] == player &&
                            field[x - 3, y + 3, z + 3] == player)
                            return ConditionResult.WinOpportunity;
                    }
                    else if (z == 3)
                    {
                        if (field[x - 1, y + 1, z - 1] == player &&
                            field[x - 2, y + 2, z - 2] == player &&
                            field[x - 3, y + 3, z - 3] == player)
                            return ConditionResult.WinOpportunity;
                    }
                }
                else if (y == 3)
                {
                    if (z == 0)
                    {
                        if (field[x - 1, y - 1, z + 1] == player &&
                            field[x - 2, y - 2, z + 2] == player &&
                            field[x - 3, y - 3, z + 3] == player)
                            return ConditionResult.WinOpportunity;
                    }
                    else if (z == 3)
                    {
                        if (field[x - 1, y - 1, z - 1] == player &&
                            field[x - 2, y - 2, z - 2] == player &&
                            field[x - 3, y - 3, z - 3] == player)
                            return ConditionResult.WinOpportunity;
                    }
                }
            }
            return ConditionResult.None;
        }
    }
}
